package com.example.paimengshop;

import com.example.paimengshop.client_socket.ActionControl_Send;
import com.example.paimengshop.dataclass.Commodity;
import com.example.paimengshop.dataclass.Order;
import com.example.paimengshop.mypanes.CartPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class cartController implements Initializable {
    @FXML
    private AnchorPane myPane;

    @FXML
    private ImageView titleImage;

    @FXML
    private ImageView cartTitle;

    @FXML
    private VBox vBox;//用于添加购物车商品的布局

    @FXML
    private RadioButton chooseAll;

    @FXML
    private Button deleteButton;

//    @FXML
//    private Label priceLabel;

    private ArrayList<CartPane> cartPanes = new ArrayList<>();

    @FXML
    private Button buyButton;



    @Override
    public void initialize(URL url, ResourceBundle resourceBundle)
    {
        myPane.getStylesheets().add(this.getClass().getResource("All.css").toExternalForm());

        chooseAll.getStyleClass().add("radio-button");

        deleteButton.getStyleClass().add("btn-basic");
        buyButton.getStyleClass().add("btn-basic");

        //控件样式
        titleImage.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/beautifyImage/titleImage.png"));

        cartTitle.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/beautifyImage/cartTitle.png"));


        //加载购物车商品列表
        vBox.getChildren().clear();
        cartPanes.clear();

        vBox.setPrefHeight(Data.currentUser.getCart().size() * 200);
        vBox.setAlignment(Pos.CENTER);


        for (int i = 0; i < Data.currentUser.getCart().size(); i++) {


            CartPane cartPane = new CartPane(Data.currentUser.getCart().get(i));
            vBox.getChildren().add(cartPane);
            cartPanes.add(cartPane);

        }

        //太难，不做了
        //价钱随时更新
//        double price=0;
//        for(int i=0;i<cartPanes.size();i++){
//            if(cartPanes.get(i).isSelected()){
//                price=price+cartPanes.get(i).getCommodity().getPrice();
//            }
//        }
//        priceLabel.setText(price+"元");


        //鼠标点击事件
        //全选购物车
        chooseAll.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if(chooseAll.isSelected())
                {
                    for (int i = 0; i < Data.currentUser.getCart().size(); i++)
                    {
                        cartPanes.get(i).getCheckBox().setSelected(true);
                    }
                }
                else
                {
                    for (int i = 0; i < Data.currentUser.getCart().size(); i++)
                    {
                        cartPanes.get(i).getCheckBox().setSelected(false);
                    }
                }

            }
        });

        //删除购物车中的商品
        deleteButton.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event) {
            for(int i = 0; i < cartPanes.size(); i++)
            {
                if(cartPanes.get(i).getCheckBox().isSelected())
                {
                    Data.currentUser.getCart().remove(cartPanes.get(i).getCommodity());
                }
            }

                main.getSend().Send(ActionControl_Send.UpdateCart(Data.currentUser, Data.currentUser.getCart()));

                vBox.getChildren().clear();
                cartPanes.clear();
                int number = Data.currentUser.getCart().size();
                vBox.setPrefHeight(number * 200);
                vBox.setAlignment(Pos.CENTER);


                for (int i = 0; i < number; i++) {


                    CartPane cartPane = new CartPane(Data.currentUser.getCart().get(i));
                    vBox.getChildren().add(cartPane);
                    cartPanes.add(cartPane);

                }
            }
        });

        buyButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event)
            {
                ArrayList<Commodity> commodities = new ArrayList<>();
                Double payment = 0.0;
                int commodityNumber = 0;
                for(int i = 0; i < Data.currentUser.getCart().size(); i++)
                {
                    if(cartPanes.get(i).getCheckBox().isSelected())
                    {
                        commodities.add(cartPanes.get(i).getCommodity());
                        payment += cartPanes.get(i).getCommodity().getPrice();
                        commodityNumber++;
                    }
                }
                Order order = new Order(-1, commodities, Data.currentUser.getId(),
                        payment, Data.currentUser.getAddress(), commodityNumber);

                main.getSend().Send(ActionControl_Send.CreateOrder(Data.currentUser, order));

                Data.orders.add(order);
            }
        });


    }
}
